using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace Acms.AcmsControls
{
    public abstract class BaseContentControlEdit : CompositeControl
    {
        public enum States
        {
            Display,
            Edit
        }

        private States state;
        public States State
        {
            get { return state; }
            set { state = value; }
        }

        protected override object SaveViewState()
        {
            ViewState["State"] = state;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            if(ViewState["State"] is States)
                state = (States)ViewState["State"];
        }

        private PlaceHolder phDisplay;
        private PlaceHolder phEdit;
        protected sealed override void CreateChildControls()
        {
            phDisplay = new PlaceHolder();
            this.Controls.Add(phDisplay);
            CreateDisplayControls(phDisplay);

            phEdit = new PlaceHolder();
            this.Controls.Add(phEdit);
            CreateEditControls(phEdit);

            base.CreateChildControls();
        }

        protected abstract void CreateDisplayControls(PlaceHolder placholder);
        protected abstract void CreateEditControls(PlaceHolder placholder);
        protected abstract BaseContentControlProperties GetProperties();
        protected abstract void SetProperties(BaseContentControlProperties properties);
        protected abstract void SaveToProperties();
        protected abstract void DeleteFromProperties();

        private void LoadData(System.Xml.XmlDocument xmlDocument)
        {
            if (xmlDocument != null)
            {
                BaseContentControlProperties tmpProp = GetProperties();
                System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(tmpProp.GetType());
                SetProperties((BaseContentControlProperties)xmls.Deserialize(new System.IO.StringReader(xmlDocument.OuterXml)));
            }
        }

        public void LoadData(Acms.Bll.PostControl postControl)
        {
            try
            {
                LoadData(postControl.Value);
            }
            catch (System.Xml.XmlException e)
            {
                throw new Acms.Exceptions.ContentControlException(string.Format("Failed to load Xml Data for PostControlId '{0}' : {1}", postControl.Id, e.Message));
            }
        }

        public void LoadData(Acms.Bll.UnusedPostControl postControl)
        {
            try
            {
                LoadData(postControl.Value);
            }
            catch (System.Xml.XmlException e)
            {
                throw new Acms.Exceptions.ContentControlException(string.Format("Failed to load Xml Data for PostControlId '{0}' : {1}", postControl.Id, e.Message));
            }
        }

        public override void DataBind()
        {
            EnsureChildControls();
            base.DataBind();
        }

        public void SaveData(Acms.Bll.PostControl postControl)
        {
            LoadData(postControl);
            EnsureChildControls();
            SaveToProperties();
            BaseContentControlProperties tmpProp = GetProperties();

            System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(tmpProp.GetType());
            System.IO.MemoryStream memStream = new System.IO.MemoryStream();
            System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter(memStream, System.Text.Encoding.UTF8);
            xmls.Serialize(memStream, tmpProp);
            memStream.Close();
            postControl.Value = new System.Xml.XmlDocument();
            postControl.Value.LoadXml(System.Text.Encoding.UTF8.GetString(memStream.ToArray()));
            postControl.Update();
        }

        public void DeleteData(Acms.Bll.PostControl postControl)
        {
            LoadData(postControl);
            DeleteFromProperties();
            postControl.Delete();
        }
        
        public override void RenderControl(System.Web.UI.HtmlTextWriter writer)
        {
            if (this.State == States.Edit)
            {
                phEdit.RenderControl(writer);
            }
            else
            {
                phDisplay.RenderControl(writer);
            }
        }

    }
}
